package com.saturdaycoder.volumeman;

import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class Database extends SQLiteOpenHelper {
	private static final String DATABASE_NAME = "userdb.sqlite";
	private static final int DB_VERSION = 1;
	
	private static final String TABNAME_PROFILES = "profiles";
	
	private static final String COLNAME_ID = "id";
	private static final String COLNAME_VOICE = "voice";
	private static final String COLNAME_MEDIA = "media";
	private static final String COLNAME_RINGER = "ringer";
	private static final String COLNAME_RINGER_VIBRATE = "ringer_vibrate";
	private static final String COLNAME_USE_RINGER_AS_ALERT = "use_ringer_as_alert";
	private static final String COLNAME_ALERT = "alert";
	private static final String COLNAME_ALERT_VIBRATE = "alert_vibrate";
	private static final String COLNAME_SYSTEM = "system";
	private static final String COLNAME_ALARM = "alarm";
	
	
	
	
	protected Database(Context context) {
		super(context, DATABASE_NAME, null, DB_VERSION);
	}
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE " + TABNAME_PROFILES + " ("
				+ COLNAME_ID + " INTEGER primary key, "
				+ COLNAME_VOICE + " INTEGER DEFAULT 7, "
				+ COLNAME_MEDIA + " INTEGER DEFAULT 10, "
				+ COLNAME_RINGER + " INTEGER DEFAULT 6, "
				+ COLNAME_RINGER_VIBRATE + " INTEGER DEFAULT 1, "
				+ COLNAME_USE_RINGER_AS_ALERT + " INTEGER DEFAULT 0, "
				+ COLNAME_ALERT + " INTEGER INTEGER 6, "
				+ COLNAME_ALERT_VIBRATE + " INTEGER DEFAULT 0, "
				+ COLNAME_SYSTEM + " INTEGER DEFAULT 5, "
				+ COLNAME_ALARM + " INTEGER DEFAULT 7)");
		setProfile(0, Profile.defaultProfiles[0]);
		setProfile(1, Profile.defaultProfiles[1]);
		setProfile(2, Profile.defaultProfiles[2]);
		setProfile(3, Profile.defaultProfiles[3]);
		setProfile(4, Profile.defaultProfiles[4]);
	}
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
	{
		db.execSQL("DROP TABLE IF EXISTS channels");
		onCreate(db);
	}
	
	public int setProfile(int index, Profile profile) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(COLNAME_ID, index);
		values.put(COLNAME_VOICE, profile.voice);
		values.put(COLNAME_MEDIA, profile.media);
		values.put(COLNAME_RINGER, profile.ringer);
		values.put(COLNAME_RINGER_VIBRATE, profile.vibrateRinger? 1: 0);
		values.put(COLNAME_USE_RINGER_AS_ALERT, profile.useRingerAsAlert? 1: 0);
		values.put(COLNAME_ALERT, profile.alert);
		values.put(COLNAME_ALERT_VIBRATE, profile.vibrateAlert? 1: 0);
		values.put(COLNAME_SYSTEM, profile.system);
		values.put(COLNAME_ALARM, profile.alarm);		
		
		long res = db.insertOrThrow(TABNAME_PROFILES, null, values);
		if (res == -1) {
			ContentValues valuesUpdate = new ContentValues();
			valuesUpdate.put(COLNAME_VOICE, profile.voice);
			valuesUpdate.put(COLNAME_MEDIA, profile.media);
			valuesUpdate.put(COLNAME_RINGER, profile.ringer);
			valuesUpdate.put(COLNAME_RINGER_VIBRATE, profile.vibrateRinger? 1: 0);
			valuesUpdate.put(COLNAME_USE_RINGER_AS_ALERT, profile.useRingerAsAlert? 1: 0);
			valuesUpdate.put(COLNAME_ALERT, profile.alert);
			valuesUpdate.put(COLNAME_ALERT_VIBRATE, profile.vibrateAlert? 1: 0);
			valuesUpdate.put(COLNAME_SYSTEM, profile.system);
			valuesUpdate.put(COLNAME_ALARM, profile.alarm);
			
			return db.update(TABNAME_PROFILES, valuesUpdate, COLNAME_ID + "=?", 
					new String[]{ String.valueOf(index)});
		} else {
			return 1;
		}	
	}
	
	public Profile getProfile(int index) {
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from " + TABNAME_PROFILES + " where " 
						+ COLNAME_ID + "=" + index, null);
		if (cursor.moveToFirst()) {
			Profile p = new Profile();
			p.voice = cursor.getInt(1);
			p.media = cursor.getInt(2);
			p.ringer = cursor.getInt(3);
			p.vibrateRinger = (cursor.getInt(4) == 1);
			p.useRingerAsAlert = (cursor.getInt(5) == 1);
			p.alert = cursor.getInt(6);
			p.vibrateAlert = (cursor.getInt(7) == 1);
			p.system = cursor.getInt(8);
			p.alarm = cursor.getInt(9);
			return p;
		}
		else {
			return null;
		}
	}
}
